//
//  WeiboCellLayout.m
//  Project_2_weibo
//
//  Created by 林林 on 16/6/20.
//  Copyright © 2016年 lhn. All rights reserved.
//

#import "WeiboCellLayout.h"
#import "WXLabel.h"

@implementation WeiboCellLayout

+ (instancetype)weiboCellLayoutWithWeiboModel:(Weibo *)weiboModel {
    
    WeiboCellLayout *layout = [[self alloc] init];
    layout.weiboModel = weiboModel;
    
    return layout;
}

//当设置新对象时需要布局
- (void)setWeiboModel:(Weibo *)weiboModel {
    
    if (_weiboModel == weiboModel) {
        return;
    }
    _weiboModel = weiboModel;
    
    //固定的大小
    _cellHeight = kTopViewHeight + kSpaceWidth;
    
    //------------------------------微博正文-------------------------------------
    //计算正文高度
    //正文Label高度
//    CGSize size = CGSizeMake(kScreenWidth - kSpaceWidth * 2, 10000);
//    NSDictionary *attributes = @{NSFontAttributeName : kWieboTextFont};
//    //计算Label大小
//    CGRect rect = [_weiboModel.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    
    CGFloat weiboTextHeight = [WXLabel getTextHeight:kWieboTextFont.pointSize width:kScreenWidth - kSpaceWidth * 2 text:_weiboModel.text linespace:kWeiboTextLinespace];
    
    //计算正文Frame
    _weiboTextFrame = CGRectMake(kSpaceWidth, kTopViewHeight + kSpaceWidth, kScreenWidth - 2 * kSpaceWidth, weiboTextHeight);
    //将正文的高度 累加到总高度上
    _cellHeight = _cellHeight + _weiboTextFrame.size.height;
    
    //--------------------------------转发微博--------------------------------
    //判断是否有转发微博
    if (weiboModel.retweeted_status) {
        //计算转发微博的大小
        //计算label大小
//        CGSize size = CGSizeMake(kScreenWidth - kSpaceWidth * 2, 10000);
//        NSDictionary *attributes = @{NSFontAttributeName : kReWieboTextFont};
//        //计算Label大小
//        CGRect rect = [_weiboModel.retweeted_status.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
        
        CGFloat weiboReTextHeight = [WXLabel getTextHeight:kReWieboTextFont.pointSize width:kScreenWidth - kSpaceWidth * 2 text:_weiboModel.retweeted_status.text linespace:kReWeiboTextLinespace];
        
        //计算正文Frame
        _reWeiboTextFrame = CGRectMake(kSpaceWidth + kReWeiboSpace, CGRectGetMaxY(_weiboTextFrame) + kSpaceWidth + kReWeiboSpace, kScreenWidth - 2 * (kSpaceWidth + kReWeiboSpace), weiboReTextHeight);
        //计算背景图大小
        _reWeiboBgImageViewFrame = _reWeiboTextFrame;
        _reWeiboBgImageViewFrame.size.width += 2 * kReWeiboSpace;
        _reWeiboBgImageViewFrame.size.height += 2 * kReWeiboSpace;
        _reWeiboBgImageViewFrame.origin.x -= kReWeiboSpace;
        _reWeiboBgImageViewFrame.origin.y -= kReWeiboSpace;
        
        _cellHeight += _reWeiboBgImageViewFrame.size.height +kReWeiboSpace;
        
        //转发微博的多图
        if (weiboModel.retweeted_status.bmiddle_pic) {
            CGFloat imageHeight = [self layoutNineImageViewFrameWithImageCount:weiboModel.retweeted_status.pic_urls.count viewWidth:(kScreenWidth - 2 *(kSpaceWidth + kReWeiboSpace)) top:(CGRectGetMaxY(_reWeiboTextFrame) + kReWeiboSpace)];
            
            _cellHeight += imageHeight + kReWeiboSpace;
            _reWeiboBgImageViewFrame.size.height += imageHeight +kReWeiboSpace;
        }
        _weiboImageViewFrame = CGRectZero;
    }
    //------------------------------正文图片-------------------------------------
    else if (_weiboModel.bmiddle_pic) {
        //没有转发的微博,再来判断是否有图片
//        _weiboImageViewFrame = CGRectMake(kSpaceWidth, CGRectGetMaxY(_weiboTextFrame) + kSpaceWidth, kImageViewWidth, kImageViewWidth);
        
        //布局九个图片
        CGFloat imageHeight = [self layoutNineImageViewFrameWithImageCount:_weiboModel.pic_urls.count viewWidth:(kScreenWidth - 2 * kSpaceWidth) top:(CGRectGetMaxY(_weiboTextFrame) + kSpaceWidth)];
        
        //图片高度加到总高度
        _cellHeight = _cellHeight + imageHeight + kSpaceWidth;
        
        _reWeiboTextFrame = CGRectZero;
        _reWeiboBgImageViewFrame = CGRectZero;
    } else {
        _weiboImageViewFrame = CGRectZero;
        _reWeiboTextFrame = CGRectZero;
        _reWeiboBgImageViewFrame = CGRectZero;
    }
    
    _cellHeight = _cellHeight +kSpaceWidth;
}

//多图微博
- (CGFloat)layoutNineImageViewFrameWithImageCount:(NSInteger)imageCount viewWidth:(CGFloat)width top:(CGFloat)top {
    
    //图片数量不能大于9
    if (imageCount > 9) {
        return 0;
    }
    
    //分多种情况计算高度
    CGFloat imageWidth = 0;
    CGFloat height = 0;
    
    if (imageCount == 1) {
        //一行一列
        imageWidth = width / 3;
        height = imageWidth;
    } else if (imageCount == 2) {
        //一行两列
        imageWidth = (width - kReWeiboSpace) / 2;
        height = imageWidth;
    } else if (imageCount == 4) {
        //两行两列
        imageWidth = (width - kReWeiboSpace) / 2;
        height = imageWidth * 2 + kReWeiboSpace;
    } else {
        //三列
        if (imageCount == 3) {
            //一行
            imageWidth = (width - kReWeiboSpace * 2) / 3;
            height = imageWidth;
        } else if (imageCount <= 6) {
            //两行
            imageWidth = (width - kReWeiboSpace * 2) / 3;
            height = imageWidth * 2 + kReWeiboSpace;
        } else {
            //三行
            imageWidth = (width - kReWeiboSpace * 2) / 3;
            height = imageWidth * 3 + kReWeiboSpace * 2;
        }
    }
    
    //初始化容器
    NSMutableArray *mArray = [[NSMutableArray alloc] init];
    
    //计算frame
    for (int i = 0; i < 9; i++) {
        //超出图片个数
        if (i >= imageCount) {
            CGRect frame = CGRectZero;
            [mArray addObject:[NSValue valueWithCGRect:frame]];
        }else {
            //分别计算九个视图的大小和位置
            NSInteger number = (imageCount == 2 || imageCount == 4) ? 2 : 3;
            //计算行列号
            NSInteger row = i / number;
            NSInteger column = i %number;
            CGFloat speace = (kScreenWidth - width) / 2;
            //计算frame
            CGRect frame = CGRectMake(column * (imageWidth + kReWeiboSpace) + speace, row * (imageWidth + kReWeiboSpace) + top, imageWidth, imageWidth);
            [mArray addObject:[NSValue valueWithCGRect:frame]];
        }
        _imagesFrameArray = [mArray copy];
    }
    
    return height;
}


@end








